package com.zkca.xf.presenter;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.BaiDuRouteBean;
import com.zkca.xf.view.BaiDuRouteView;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pc on 2018/8/22.
 */

public class BaiDuRoutePresenter extends BasePresenter {
    private BaiDuRouteView baiDuRouteView;
    private DataManager dataManager;
    public BaiDuRoutePresenter(LifecycleProvider<ActivityEvent> provider,BaiDuRouteView baiDuRouteView) {
        super(provider);
        dataManager= DataManager.getInstance();
        this.baiDuRouteView=baiDuRouteView;
    }

    public void BaiDuRoute(Map<String, String> map) {
        dataManager.getBaiDuRoute(map)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<BaiDuRouteBean>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<BaiDuRouteBean>() {// 订阅
                    @Override
                    public void onNext(BaiDuRouteBean dataBean) {
                        baiDuRouteView.getBaiDuRoute(dataBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        baiDuRouteView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        baiDuRouteView.hideProgressDialog();
                    }
                });
    }

}
